﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ComicsEditor
{
    public partial class RenderDialog : Form
    {
        private ComicInfo comicInfo;

        public RenderDialog()
        {
            InitializeComponent();
        }

        public ComicInfo ComicInfo
        {
            set
            {
                comicInfo = value;
            }
        }

        public string DefaultPath
        {
            set
            {
                txtFolder.Text = value;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            WriterFolder writer = new WriterFolder(txtFolder.Text);
            writer.AddStream("ComicInfo.xml", new MemoryStream(Encoding.UTF8.GetBytes(comicInfo.ToXml())));
            for (int i = 0; i < comicInfo.Count; i++)
            {
                PageInfo pageInfo = comicInfo[i];
                RectangleF oldOutput = pageInfo.Page.Output;
                List<EditableObject> edits = new List<EditableObject>();
                foreach (PageObject obj in pageInfo.Page.Objects)
                {
                    EditableObject editObj = obj as EditableObject;
                    if (editObj != null && editObj.Editing)
                    {
                        editObj.EndEdit();
                        edits.Add(editObj);
                    }
                }
                pageInfo.Page.Output = new RectangleF(0, 0, pageInfo.Page.Width, pageInfo.Page.Height);

                Bitmap bmp = new Bitmap(pageInfo.Page.Width, pageInfo.Page.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImageUnscaled(comicInfo.GetImage(i), 0, 0);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                pageInfo.Page.Draw(g);

                pageInfo.Page.Output = oldOutput;
                foreach (EditableObject editObj in edits)
                    editObj.BeginEdit();

                MemoryStream jpeg = new MemoryStream();
                SaveJpeg(jpeg, bmp, (long)numQuality.Value);
                jpeg.Position = 0;
                writer.AddStream(pageInfo.SaveKey ?? pageInfo.Key ?? pageInfo.ViewName + ".jpg", jpeg);
            }
        }

        private void SaveJpeg(Stream stream, Image img, long nQuality)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            EncoderParameters encoderParameters = new EncoderParameters(1);
            encoderParameters.Param[0] = new EncoderParameter(global::System.Drawing.Imaging.Encoder.Quality, nQuality);

            foreach (ImageCodecInfo encoder in encoders)
                if (encoder.MimeType == "image/jpeg")
                {
                    img.Save(stream, encoder, encoderParameters);
                    return;
                }
            throw new Exception("Jpeg encoder not found.");
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = txtFolder.Text;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
